package com.zijiebutiao.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.zijiebutiao.utils.CodeFactory;
import com.zijiebutiao.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Date 2023/3/3 13:27
 * @Author 时间刺客
 */
@RestController
@Api(tags = "获取验证码")
public class CreateCodeController {
    @GetMapping("/code")
    @ApiOperation(value = "获取图片验证码",notes = "",response = ResultVo.class)
    public ResultVo getCode() throws IOException {
        BASE64Encoder encoder = new BASE64Encoder();
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 40, 4, 30);
        Map<String,String> data = new HashMap<>();
        data.put("image",lineCaptcha.getImageBase64());
        data.put("codepwd",encoder.encode(lineCaptcha.getCode().getBytes()));
        data.put("code",lineCaptcha.getCode());
        String idcode=UUID.randomUUID().toString();
        data.put("idcode",idcode);
        CodeFactory.codesmap.put(idcode,lineCaptcha.getCode());
        return ResultVo.success(data);
    }
}
